#!/usr/bin/env python
#coding:utf-8

import sys
from PySide import QtCore
from PySide import QtGui

class mywindow(QtGui.QMainWindow):
    sequenceNumber = 1
    windowList = []

    def __init__(self):
        super(mywindow, self).__init__()
        self.init()
        self.setCurrentFile('')

    def init(self):
        #Makes Qt delete this widget when the widget has accepted the close event
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.isUntitled = True
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

        self.createActions()
        self.createMenus()
        self.createToolBars()
        self.createStatusBar()

        self.readSettings()

    def createActions(self):
        self.newAct = QtGui.QAction(QtGui.QIcon('res/new.png'), "&New",
                self, shortcut=QtGui.QKeySequence.New,
                statusTip="Create a new file", triggered=self.newFile)

        self.closeAct = QtGui.QAction("&Close", self, shortcut="Ctrl+W",
                statusTip="Close this window", triggered=self.close)

        self.exitAct = QtGui.QAction("E&xit", self, shortcut="Ctrl+Q",
                statusTip="Exit the application",
                triggered=QtGui.qApp.closeAllWindows)

    def createMenus(self):
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction(self.newAct)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.closeAct)
        self.fileMenu.addAction(self.exitAct)

    def createToolBars(self):
        self.fileToolBar = self.addToolBar("File")
        self.fileToolBar.addAction(self.newAct)

    def createStatusBar(self):
        self.statusBar().showMessage("Ready")

    def readSettings(self):
        #position of saving in regedit:hkey_current_user\software\?
        settings = QtCore.QSettings('myapp', 'pyside sdi example')
        pos = settings.value('pos', QtCore.QPoint(200, 200))
        size = settings.value('size', QtCore.QSize(400, 400))
        self.move(pos)
        self.resize(size)

    def writeSettings(self):
        settings = QtCore.QSettings('myapp', 'pyside sdi example')
        settings.setValue('pos', self.pos())
        settings.setValue('size', self.size())

    def newFile(self):
        other = mywindow()
        mywindow.windowList.append(other)
        other.move(self.x() + 40, self.y() + 40)
        other.show()

    def closeEvent(self, event):
        self.writeSettings()
        event.accept()

    def setCurrentFile(self, fileName):
        if self.isUntitled:
            self.curFile = "document%d.txt" % mywindow.sequenceNumber
            #why the statement below is invalid?
            mywindow.sequenceNumber += 1
        self.setWindowTitle("%s[*] - SDI" % self.curFile)

def main():
    '''
    main function
    '''
    app = QtGui.QApplication(sys.argv)
    window = mywindow()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()
